package com.rub.android.utils

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup

class JsoupUtil {


    suspend fun refreshVideoLists() = fetchVideo()


    private suspend fun fetchVideo() = withContext(Dispatchers.IO) {
        coroutineScope {

            val get = withContext(Dispatchers.Default) {
                Jsoup.connect("https://sample-videos.com/index.php#sample-mp4-video")
                    .timeout(10 * 1000).get()
            }

            var bodyEle = get.body().getElementById("sample-mp4-video").select("tbody").first()

            val baseUrl = "https://sample-videos.com/"

            var dataList = ArrayList<Video>()
            var id = 0
            bodyEle.select("tr").forEach {
                var tdEle = it.select("td")
                if (tdEle.size > 3) {
                    id++
                    val videoUrl = baseUrl + tdEle[3].select("a").attr("href")
                    dataList.add(Video(id, "海洋的动漫$id", videoUrl, ""))
                }
            }
            dataList
        }
    }


}

data class Video(val id: Int, val videoName: String, val videoUrl: String, val videoImg: String)